import request from '../../utils/request'
import {requestString} from '../../utils/request'

/**
 * 分页查询订单
 *
 * @param customerNo
 * @param customerName
 * @param orderNo
 * @param contractNo
 * @param declarationType
 * @param status
 * @param page
 * @returns {Promise.<Object>}
 */
export async function queryOrders({ applyFlag, cstName, orderNo, contractNo, declarationType, status, page, beginTime, endTime }) {
    return request(apiPath.mc + "/api/orders", {
        method: 'GET',
        data: { applyFlag, cstName, orderNo, contractNo, declarationType, status, page: page.page, size: page.size, beginTime, endTime }
    });
}

/**
 * 根据id获取订单详情
 * @param id
 * @returns {Promise.<Object>}
 */
export async function getOrderById({id}) {
    return request(apiPath.mc + "/api/order/" + id, {
        method: 'get'
    });
}

/**
 * 查询开票人
 *
 * @param cstId
 * @param status
 * @returns {Promise.<Object>}
 */
export async function queryDrawers({ cstId, status }) {
    return request(apiPath.mc + "/api/drawers/all", {
        method: 'GET',
        data: { cstId, status }
    });
}

/**
 * 查询联系人
 *
 * @param cstId
 * @param status
 * @returns {Promise.<Object>}
 */
export async function queryLinkmans({ cstId, status }) {
    return request(apiPath.mc + "/api/" + cstId + "/linkmans", {
        method: 'GET',
        data: { status }
    });
}

/**
 * 查询货物存放地址
 *
 * @param cstId
 * @param status
 * @returns {Promise.<Object>}
 */
export async function queryAddresses({ cstId, status }) {
    return request(apiPath.mc + "/api/" + cstId + "/goodsaddress", {
        method: 'GET',
        data: { status }
    });
}

/**
 * 查询境外买家
 *
 * @param cstId
 * @param status
 * @returns {Promise.<Object>}
 */
export async function queryBuyers({ cstId, status }) {
    return request(apiPath.mc + "/api/buyers/all", {
        method: 'GET',
        data: { cstId, status }
    });
}

/**
 * 修改订单
 * @param order
 * @returns {Promise.<Object>}
 */
export async function updateOrder({order}) {
    return requestString(apiPath.mc + "/api/order", {
        method: 'PUT',
        data: {...order}
    });
}

/**
 * 根据客户查询订单境内货源地
 *
 * @param cstId
 * @returns {Promise.<Object>}
 */
export async function querySupplyGoods({cstId}) {
    return request(apiPath.mc + "/api/drawer/supplygoods", {
        method: 'GET',
        data: {cstId, status: '4'}
    });
}

/**
 * 分页查询费用列表
 *
 * @param id
 * @param status
 * @param costPage
 * @returns {Promise.<Object>}
 */
export async function queryOrderCost({ orderId, status, costPage }) {
    return request(apiPath.mc + "/api/ordercharges", {
        method: 'GET',
        data: { orderId, status, page: costPage.page, size: costPage.size }
    });
}

/**
 * 添加费用
 *
 * @param orderId
 * @param cost
 * @returns {Promise.<Object>}
 */
export async function addOrderCost({orderId, cost}) {
    return request(apiPath.mc + "/api/order/" + orderId + "/ordercharge", {
        method: 'POST',
        data: cost
    });
}

/**
 * 删除费用
 *
 * @param id
 * @returns {Promise.<Object>}
 */
export async function deleteOrderCost({ id}) {
    return request(apiPath.mc + "/api/ordercharge/" + id, {
        method: 'DELETE',
    });
}

/**
 * 申报/审批 费用
 *
 * @param id
 * @returns {Promise.<Object>}
 */
export async function applyOrderCost({cost}) {
    return request(apiPath.mc + "/api/ordercharge/apply", {
        method: 'PUT',
        data: cost
    });
}

/**
 * 确认费用收齐
 *
 * @param id
 * @param status {1: 通关收齐, 2:跟单收齐}
 * @returns {Promise.<Object>}
 */
export async function confirmCost({id, status}) {
    return request(apiPath.mc + "/api/order/" + id + "/collectedordercharge", {
        method: 'PUT',
        data: {status}
    });
}

/**
 * 通关-退回订单
 *
 * @param id
 * @param gobackreason
 * @returns {Promise.<Object>}
 */
export async function returnOrder({id, gobackreason}) {
    return request(apiPath.mc + "/api/order/" + id + "/returnorder", {
        method: 'PUT',
        data: {gobackreason}
    });
}

/**
 * 通关-订单受理
 *
 * @param id
 * @param gobackreason
 * @returns {Promise.<Object>}
 */
export async function approvalOrder({approvalOrder}) {
    return request(apiPath.mc + "/api/order/tgapproval", {
        method: 'PUT',
        data: approvalOrder
    });
}

/**
 * 通关-生成报关资料
 *
 * @param id
 * @param orderTgApproval 通关信息
 * @returns {Promise.<Object>}
 */
export async function geneateCD(data) {
    return request(apiPath.mc + "/api/order/" + data.orderId + "/declaringorder", {
        method: 'GET',
        data: {...data}
    });
}

/**
 * 通关-修改通关信息
 *
 * @param id
 * @returns {Promise.<Object>}
 */
export async function updateOrderTgApproval({orderTgApproval}) {
    return request(apiPath.mc + "/api/order/modifytginfoorder", {
        method: 'PUT',
        data: orderTgApproval
    });
}

/**
 * 查询通关专员
 *
 * @param cstId
 * @param status
 * @returns {Promise.<Object>}
 */
export async function queryTgManagers() {
    return request(apiPath.authcenter + "/api/user/tgmanager", {
        method: 'GET',
    });
}